package com.lminkey.servlet;

import com.lmonkey.servic.USERDao;
import com.lmonkey.shop.USER;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/admin/admin_douserselect")
public class DoUserSelect extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //制作分页
      int cpage = 1;//当前页
      int count = 1;//每页显示
        //获取用户指定页面(从user页面获取)
        String cp = request.getParameter("cp");
        //获取用户输入的关键字
        String keyword = request.getParameter("keywords");

      if (cp!=null)
      {
          cpage = Integer.parseInt(cp);
      }
      int arr[] = USERDao.totalPage(count,keyword);


        //获取指定页数的所以用户记录（反馈给user页面）
        ArrayList<USER> list = USERDao.selectALL(cpage,count,keyword);
       //放到请求对象域里
        request.setAttribute("userlist",list);
        request.setAttribute("tsum",arr[0]);

        //创建页码分页
      int begin = 1;
      int end = 1;
      //如果总页数小于10
      if (arr[1] <= 10){
        begin = 1;
        end = arr[1];
      }
      //如果总页数大于10
      if (arr[1] > 10){
        begin = cpage - 5;
        end = cpage + 4;

        if (begin <= 0){
          begin = 1;
          end = 10;
        }
        if (end >= arr[1]){
          begin = arr[1]-9;
          end = arr[1];
        }

      }

        request.setAttribute("tpage",arr[1]);
        request.setAttribute("cpage",cpage);
        request.setAttribute("begin",begin);
        request.setAttribute("end",end);

        if (keyword != null)
        {
            request.setAttribute("searchParams","&keywords="+keyword);
        }
        request.getRequestDispatcher("admin_user.jsp").forward(request,response);
    }
}
